<template>
  <div class="box_max">
    <el-upload
      :show-file-list="false"
      :http-request="httpRequest"
      action="#"
      class="upload"
    >
      <img v-if="value" v-imgerror :src="value" class="avatar">
      <i v-else ref="color_bs" class="el-icon-plus " />
    </el-upload>
    <i class="el-icon-delete weiyi" @click="$emit('input','')" />
    <i class="el-icon-zoom-in fangda" @click="isShow=true" />

    <el-dialog
      title="图片浏览"
      :visible.sync="isShow"
      width="80%"
    >
      <img v-imgerror :src="value" style="width:100%">
    </el-dialog>
    <!-- 进度条 -->
    <el-progress v-if="showProgress" type="circle" :percentage="percentage" class="progre" />
  </div>
</template>

<script>

import COS from 'cos-js-sdk-v5'
const cos = new COS({
  SecretId: 'AKIDHg1tdEhV5b0FKMDuOd8Sckj9PSUUMp8M',
  SecretKey: 'B8sQkslUDj4FJKSDwd5MTO3Wo0AJZnzR'
})
export default {
  name: 'UploadImg',
  components: {

  },
  props: {
    value: {
      type: String,
      default: ''
    }
  },

  data() {
    return {
      imageUrl: '',
      isShow: false,
      percentage: 0,
      showProgress: false
    }
  },
  methods: {
    beforeUpload(file) {
      const limitType = file.type === 'image/jpeg' || file.type === 'image/png'
      if (!limitType) {
        this.$message.error('请上传jpg 或 png图片')
      }
      return limitType
    },
    httpRequest(res) {
      const file = res.file /* 下面的都可以复制  这里给下面找好file就行了 */
      cos.uploadFile({
        Bucket: 'heima-1317620039', /* 存储桶，必须字段 */
        Region: 'ap-guangzhou', /* 存储桶所在地域，必须字段 */
        Key: file.name, /* 存储在桶里的对象键（例如:1.jpg，a/b/test.txt，图片.jpg）支持中文，必须字段 */
        Body: file, // 上传文件对象
        SliceSize: 1024 * 1024 * 0.5, /* 触发分块上传的阈值，超过5MB使用分块上传，小于5MB使用简单上传。可自行设置，非必须 */
        onProgress: (progressData) => {
          console.log(JSON.stringify(progressData))
          /* 在这里给进度条 */
          this.showProgress = true
          this.$refs.color_bs.style.display = 'none'
          this.percentage = +(progressData.percent * 100).toFixed(2)
          if (this.percentage === 100.00) {
            this.showProgress = false
            this.$refs.color_bs.style.display = 'block'
          }
        }
      }, (err, data) => {
        if (err) {
          console.log('上传失败', err)
        } else {
          this.imageUrl = 'http://' + data.Location
          this.$emit('input', 'http://' + data.Location)
          console.log('上传成功', data)
        }
      })
    }
  }
}
</script>

<style scoped lang="scss">

.avatar{
    width: 80%;
    height: 80%;
}
.progre{
    position: absolute;
    top: 0;
    left: 50%;
    transform: translateX(-50%) translateY(-50%);
    margin-top: 50%;
    z-index: -1;

}
.box_max{
    width: 178px;
    position: relative;
    &:hover{
        .weiyi{
            color: red;
            display: block;
        }
        .fangda{
            color: red;
            display: block;
        }
    }
}
.weiyi{
    position: absolute;
    right: 10px;
    top: 6px;
    display: none;
}
.fangda{
    position: absolute;
top: 6px;
right: 40px;
display: none;
}
.upload{
    width: 180px;
    height: 180px;
    border: 1px dashed #ccc;
    display: flex;
    justify-content: center;
    align-items: center;
    &:hover{
        border: 1px dashed royalblue;
    }
}
</style>
